package com.study.data.sort;

import java.util.Comparator;

/**
 * @program: java-study-of-learner
 * @description: 冒泡排序
 * @author: future
 * @create: 2020-10-20 22:46
 **/
public class BubbleSort {

    public static void main(String[] args) {
        int[] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            double num = Math.round(Math.random() * 100);
            int n = new Double(num).intValue();
            arr[i] = n;
        }
        printList(arr);
        sort(arr);
        printList(arr);

    }

    private static void sortObj(int[] arr) {
        Comparator<Integer> c = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                o1.compareTo(o2);
                return o1.compareTo(o2);
            }
        };
    }


    /**
     * 冒泡排序
     */
    private static void sort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

    /**
     * 打印
     */
    private static void printList(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }


}
